<?php
class Tourismgoods extends CS_Controller
{
    public function _init()
    {
        $this->load->helper(array('common'));
        $this->load->library('pagination');
        $this->load->model('region_model', 'region');
        $this->load->model('tourism_goods_image_model', 'tourism_goods_image');
        $this->load->model('tourism_goods_base_model', 'tourism_goods_base');
        $this->load->model('tourism_goods_attr_model', 'tourism_goods_attr');
        $this->load->model('tourism_goods_category_model', 'tourism_goods_category');
        $this->load->model('tourism_goods_category_additive_model', 'tourism_goods_category_additive');
        $this->load->model('tourism_goods_category_attribute_model', 'tourism_goods_category_attribute');
        $this->load->model('tourism_goods_category_spec_model', 'tourism_goods_category_spec');
        $this->load->model('tourism_goods_brand_model', 'tourism_goods_brand');
        $this->load->model('tourism_freight_template_model', 'tourism_freight_template');
        $this->load->model('tourism_provider_category_model','tourism_provider_category');
        $this->load->model('user_type_model', 'user_type');
        $this->load->model('tourism_goods_profit_model', 'tourism_goods_profit');
        $this->load->model('user_model', 'user');
        $this->load->model('operate_power_log_model', 'operate_power_log');
        $this->load->model('tourism_profit_rate_model','tourism_profit_rate');
    }
    
    public function grid($pg = 1)
    {
        $getData = $this->input->get();
        if (isset($getData['excel']) && $getData['excel']== 'excel') { 
            $this->excelExport($getData);
        } else {
            $page_num = 20;
            $num = ($pg - 1) * $page_num;
            $config['first_url'] = base_url('tourismgoods/grid') . $this->pageGetParam($getData);
            $config['suffix'] = $this->pageGetParam($getData);
            $config['base_url'] = base_url('tourismgoods/grid');
            $config['total_rows'] = $this->tourism_goods_base->total($getData);
            $config['uri_segment'] = 3;
            $this->pagination->initialize($config);
            $data['pg_list'] = $this->pagination->create_links();
            $data['line_list'] = $this->tourism_goods_base->page_list($page_num, $num, $getData);
            $data['all_rows'] = $config['total_rows'];
            $data['pg_now'] = $pg;
            $data['show_flag'] = array('1' => '大首页', '2' => '商品首页','3' => '热门推荐');
            $data['is_check'] = array('1' => '待审核', '2' => '审核通过', '3' => '审核拒绝');
            $data['is_on_sale'] = array('1' => '上架', '2' => '下架');
            $data['all_region'] = $this->region->getNewRegion();
            $data['province_id'] = $this->input->get('province_id');
            $data['city_id'] = $this->input->get('city_id');
            $data['district_id'] = $this->input->get('district_id');
            $data['url'] = $this->pageGetParam($getData);
            $this->load->view('tourismgoods/grid', $data);
        }
    }
    
    /**
     * 导出
     * @param unknown $getData
     */
    public function excelExport($getData) {
        
        $result = $this->tourism_goods_base->page_list('','',$getData,$status=false);
        if ($result->num_rows() <= 0) {
            $this->error('tourismgoods/grid', null, '这个时间段没有记录');
        }
        if ($result->num_rows() > 10000) {
            $this->error('tourismgoods/grid', null, '由于导出的数据太多，请选择一个时间范围');
        }
        $arrayResult = $result->result();
        $arrayGoods = array();
        foreach ($arrayResult as $key=>$item) { 
            $arrayGoods[$key]['goods_attr_id'] = $item->goods_attr_id;
            $arrayGoods[$key]['goods_sn'] = $item->goods_sn;
            $arrayGoods[$key]['goods_name'] = $item->goods_name;
            $arrayGoods[$key]['alias_name'] = $item->alias_name;
            $arrayGoods[$key]['detail'] = $item->detail;
            $arrayGoods[$key]['supply_price'] = $item->supply_price;
            $arrayGoods[$key]['shop_price'] = $item->shop_price;
            $arrayGoods[$key]['market_price'] = $item->market_price;
            $arrayGoods[$key]['created_at'] = $item->created_at;
            $arrayGoods[$key]['updated_at'] = $item->updated_at;
        }
        array_unshift($arrayGoods, array('产品ID','编号','产品名称','供应商','省/市/区','供应价','着想价','销售价','上架时间','更新时间'));
        $this->load->library('excel');
        $this->excel->addArray($arrayGoods);
        $this->excel->generateXML(date('Ymd').'商城产品列表');
    }
    
     /**
     * shanchu
     * @param number $pg
     */
    public function delete($pg=1) {
        
        $page_num = 20;
        $num = ($pg - 1) * $page_num;
        $param['goods_search'] = $this->input->get('goods_search');
        $param['username'] = $this->input->get('username');
        $param['is_on_sale'] = $this->input->get('is_on_sale');
        $param['is_check'] = $this->input->get('is_check');
        $param['logic_delete'] = 2;
        $config['first_url'] = base_url('tourismgoods/delete') . $this->pageGetParam($param);
        $config['suffix'] = $this->pageGetParam($param);
        $config['base_url'] = base_url('tourismgoods/delete');
        $config['total_rows'] = $this->tourism_goods_base->total($param);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_list'] = $this->pagination->create_links();
        $data['line_list'] = $this->tourism_goods_base->page_list($page_num, $num, $param);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['is_check'] = array('1' => '待审核', '2' => '审核通过', '3' => '审核拒绝');
        $data['is_on_sale'] = array('1' => '上架', '2' => '下架');
        $data['all_region'] = $this->region->getNewRegion();
        $data['url'] = $this->pageGetParam($param);
        $this->load->view('tourismgoods/delete', $data);
    }

    public function addstep1()
    {
        $this->load->view('tourismgoods/addstep1');
    }

    public function addstep2()
    {
        $class = (int)$this->input->get('class_c');
        //获得末级的类别id
        if (empty($class)) {
            $this->error('tourismgoods/addstep1', '', '请选择完整的类别');
        }
        $data['category'] = $this->tourism_goods_category->getCategoryByLastId($class);//类别信息
        $data['brand'] = $this->tourism_goods_brand->getBrand($class);//品牌信息
        $data['attribute'] = $this->tourism_goods_category_attribute->getAttributeByCategoryId($class);//属性规格信息
        $data['additive'] = $this->tourism_goods_category_additive->getAdditive($class);//附加属性信息
        $data['userType'] = $this->user_type->get_list('Y', array('8192', '134217728')); //用户分润
        $data['show_flag'] = array('1' => '大首页', '2' => '商品首页','3' => '热门推荐');
        $this->load->view('tourismgoods/addstep2', $data);
    }
    
    public function addPost()
    {
        $this->db->trans_begin();
        $goods_id = $this->tourism_goods_base->insertTourismGoodsBase($this->input->post());
        $goods_attr_id = $this->tourism_goods_attr->insertTourismGoodsAttr($goods_id, $this->input->post());
        $uidArr = $this->input->post('uid');
        if (!empty($uidArr)) {
            $resultId = $this->tourism_goods_profit->add($goods_attr_id, $this->input->post());
            if (!$resultId){
                $this->db->trans_rollback();
                $this->jsen('保存失败！');
            }
        }
/*操作记录 operate_power_log*/
         $this->operate_power_log->insertOperatePowerLog(array('uid'=>$this->uid,'name'=>$this->name.'-商品','record'=>'操作类：'.__CLASS__.'-方法：'.__FUNCTION__, 'content'=>json_encode(array('goods_attr_id'=>$goods_attr_id,'goods_id'=>$goods_id,'is_check'=>$this->input->post('is_check'),'is_on_sale'=>$this->input->post('is_on_sale')))));
        if (!$goods_id && !$goods_attr_id && $this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            $this->jsen('保存失败！');
        } else {
            $this->db->trans_commit();
            $this->session->set_flashdata('success', '保存成功!');
            $this->jsen(base_url('tourismgoods/grid'), TRUE);
        }
        exit;
    }
    
    /**
     * 商品信息修改
     * author laona
     */
    public function edit($goods_attr_id = 0)
    {
        $result = $this->tourism_goods_base->getGoodsAttr($goods_attr_id);
        if ($result->num_rows() <= 0) {
            $this->error('tourismgoods/grid', '', '找不到产品相关信息！');
        }
        $tourism = $result->row();
        $category_id = $tourism->category_id;
        
        //获取附加属性
        $data['additive'] = $this->tourism_goods_category_additive->getAdditive($category_id);
        //获取分类信息 全称
        $data['category'] = $this->tourism_goods_category->getCategoryByLastId($category_id);
        //获取用户类型
        $data['userType'] = $this->user_type->get_list('Y', array('8192', '134217728')); //用户类型
        //获取分润信息
        $data['profit'] = $this->tourism_goods_profit->findByArray(array('goods_attr_id' => $goods_attr_id, 'status' => 1));
        //获取商品品牌
        $data['brand'] = $this->tourism_goods_brand->getBrand($category_id);
        $data['attribute'] = $this->tourism_goods_category_attribute->getAttributeByCategoryId($category_id);
        $data['result'] = $tourism;
        $data['template'] = $this->tourism_freight_template->getTransport($tourism->uid);
        $data['providerCategory'] = $this->tourism_provider_category->providerCategoryByUid($tourism->uid);
        $data['province_id'] = $tourism->province_id;
        $data['city_id'] = $tourism->city_id;
        $data['district_id'] = $tourism->district_id;
        $data['show_flag'] = array('1' => '大首页', '2' => '商品首页','3' => '热门推荐');
        $data['profitRate'] = $this->tourism_profit_rate->findByParams(array('uid'=>['in' => [$tourism->uid,0]]),'rate_id,uid,name',null,'rate_id');
        $this->load->view('tourismgoods/edit', $data);
    }
    
    /**
     * 商品复制
     * @param number $goods_attr_id
     */
    public function copy($goods_attr_id = 0)
    {
        $result = $this->tourism_goods_base->getGoodsAttr($goods_attr_id);
        if ($result->num_rows() <= 0) {
            $this->error('tourismgoods/grid', '', '找不到产品相关信息！');
        }
        $tourism = $result->row();
        $category_id = $tourism->category_id;
        //获取附加属性
        $data['additive'] = $this->tourism_goods_category_additive->getAdditive($category_id);
        //获取分类信息 全称
        $data['category'] = $this->tourism_goods_category->getCategoryByLastId($category_id);
        //获取用户类型
        $data['userType'] = $this->user_type->get_list('Y', array('8192', '134217728')); //用户类型
        //获取分润信息
        $data['profit'] = $this->tourism_goods_profit->findByArray(array('goods_attr_id' => $goods_attr_id, 'status' => 1));
        //获取
        $data['brand'] = $this->tourism_goods_brand->getBrand($category_id);
        $data['attribute'] = $this->tourism_goods_category_attribute->getAttributeByCategoryId($category_id);
        $data['result'] = $tourism;
        $data['template'] = $this->tourism_freight_template->getTransport($tourism->uid);
        $data['province_id'] = $tourism->province_id;
        $data['city_id'] = $tourism->city_id;
        $data['district_id'] = $tourism->district_id;
        $data['show_flag'] = array('1' => '大首页', '2' => '商品首页','3' => '热门推荐');
        $this->load->view('tourismgoods/copy', $data);
    }

    public function editPost()
    {
        $goods_attr_id = $this->input->post('goods_attr_id');
        $this->db->trans_begin();
        $updateGoods = $this->tourism_goods_base->updateTourismGoodsBase($this->input->post());
        $updateAttr = $this->tourism_goods_attr->updateTourismGoodsAttr($goods_attr_id, $this->input->post());
        
        $uidArr = $this->input->post('uid');
        if (!empty($uidArr)) {
            $is_delete = $this->tourism_goods_profit->delete(array('goods_attr_id' => $goods_attr_id, 'status' => 1));
            $resultId = $this->tourism_goods_profit->add($goods_attr_id, $this->input->post());
            if (!$is_delete && $resultId){
                $this->db->trans_rollback();
                $this->jsen('保存失败！');
            }
        }
        /*操作记录 operate_power_log*/
        $this->operate_power_log->insertOperatePowerLog(array('uid'=>$this->uid,'name'=>$this->name.'-商品','record'=>'操作类：'.__CLASS__.'-方法：'.__FUNCTION__, 'content'=>json_encode(array('goods_attr_id'=>$goods_attr_id,'is_check'=>$this->input->post('is_check'),'is_on_sale'=>$this->input->post('is_on_sale')))));
        if (!$updateGoods && !$updateAttr && $this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            $this->jsen('保存失败！');
        } else {
            $this->db->trans_complete();
            $this->session->set_flashdata('success', '保存成功!');
            $this->jsen(base_url('tourismgoods/grid'), TRUE);
        }
        exit;
    }
    
    public function ajaxValidate()
    {
        $error = $this->validate();
        if (!empty($error)) {
            $this->jsen($error);
        }
        $uidArr = $this->input->post('uid');
        if (!empty($uidArr)) {
            $error = $this->validateProfit();
            if (!empty($error)) {
                $this->jsen($error);
            }
        }
        if ($this->input->post('goods_attr_id')) {
            $this->editPost();
        } else {
            $this->addPost();
        }
    }

    private function jsen($error, $flag = false)
    {
        echo json_encode(array(
            'status'   => $flag,
            'messages' => $error
        ));
        exit;
    }

    public function validate()
    {
        $error = array();
        if ($this->validateParam($this->input->post('goods_name'))) {
            $error[] = '商品名称不可为空！';
        }
        if ($this->validateParam($this->input->post('wap_goods_desc'))) {
            $error[] = '商品详情不可为空！';
        }
        if ($this->validateParam($this->input->post('supplierUid'))) {
            $error[] = '供应商必须填写';
        }
        if ($this->input->post('weight') < 0) {
            $error[] = '商品重量必须大于等于0';
        }
        if ( !is_numeric($this->input->post('limit_num')) ||  $this->input->post('limit_num') < 0) {
            $error[] = '商限购数量必须大于或等于0';
        }
        if ($this->input->post('market_price') < 0) {
            $error[] = '市场价格必须大于等于0。';
        }
        if ($this->input->post('supply_price') < 0) {
            $error[] = '供应价格必须大于等于0.';
        }
        if ($this->input->post('shop_price') < 0) {
            $error[] = '着想价格必须大于等于0。';
        }
        if ($this->input->post('goods_num') <= 0) {
            $error[] = '库存必须大于0.';
        }
        if ($this->input->post('swim') < 0) {
            $error[] = '可用游币必须大于0。';
        }
        //验证运费模版
        if ($this->input->post('transport_type') == 1) {
            if (!$this->input->post('transport_id')) {
                $error[] = '运费模版不可不填。';
            }
        }
        
        //地区
        $regionids = array($this->input->post('province_id'), $this->input->post('city_id'), $this->input->post('district_id'));
        $region = $this->region->getByRegionIds($regionids);
        if ($region->num_rows() < 3) {
            $error[] = '城市地区请填写完整。';
        }
        $regionNames = array();
        foreach ($region->result() as $item) {
            $regionNames[] = $item->region_name;
        }
        $_POST['detail'] = $regionNames[0] .' '.$regionNames[1].' '.$regionNames[2].' '.($this->input->post('detail') ? $this->input->post('detail') : '　');
        return $error;
    }

    public function validateProfit()
    {
        $error = array();

        $typeIDArray = $this->input->post('type_id');
        $uidArray = $this->input->post('uid');
        $amountArray = $this->input->post('amount');
        if ($this->input->post('supply_price') == 0 && $this->input->post('shop_price') == 0 ) {
           if (empty($uidArray) && empty($typeIDArray) && empty($amountArray)) {
               return $error;
           } else {
               return $error[] = '请仔细核对，手续费，分润金额！';
           }
        }
        $amount = 0;        //分销商总金额
        $amountUid = 0;     //供应商总金额
        $supplyNum = 0;     //判断是否有供应商
        $purchaserNum = 0;  //判断是采购商个数
        $providerUser = array();
        if (empty($uidArray) || empty($typeIDArray) || empty($amountArray)) {
            return $error[] = '请填写分润信息！';
        }

        if (count($uidArray) != count($typeIDArray) || count($uidArray) != count($amountArray)) {
            return $error[] = '请联系管理员。';
        }

        foreach ($typeIDArray as $key => $value) {
            if (!is_numeric($amountArray[$key]) || $amountArray[$key] < 0) {
                return $error[] = '金额必须填写大于或等于0的数字！';
            }
            if ($value == UTID_PROVIDER) {
                $amountUid += $amountArray[$key];
                $providerUser[] = $uidArray[$key];
                $amount += $amountArray[$key];
            } else {
                if ($value == 4 || $value == 16) {
                    $purchaserNum += 1;
                    if (empty($uidArray[$key])) {
                        return $error[] = '请填写采购商和着想总部的帐号！';
                    }
                }
                $amount += $amountArray[$key];
            }
        }
        if (bccomp($amountUid, $this->input->post('supply_price'), 3) != 0) {
            return $error[] = '供应总金额' . $amountUid . '与供应价' . $this->input->post('supply_price') . '不符！';
        }
        if (bccomp($amount, $this->input->post('shop_price'), 3) != 0) {
            return $error[] = '分销总金额' . $amount . '与着想' . $this->input->post('shop_price') . '不符！';
        }

        //去掉为空  的用户uid
        foreach ($uidArray as $key => $value) {
            if (empty($value)) {
                unset($uidArray[$key]);
            }
        }

        $result = $this->user->findByIds(array('uid' => $uidArray));
        if (empty($result)) {
            return $error[] = '用户编号为' . implode(array_flip($uidArray), ',') . '的用户不存';
        }
        $uidArray = array_flip($uidArray);   //  key值交换

        foreach ($result->result() as $value) {
            if (in_array($value->uid, $providerUser)) {
                $supplyNum += 1;
            }
            if (array_key_exists($value->uid, $uidArray)) {
                unset($uidArray[$value->uid]);
            }
        }
        if (count($providerUser) > 1) {
            return $error[] = '请不要选择多个供应商！';
        }

        if ($supplyNum <= 0) {
            return $error[] = '请填写一个真实的供应商！';
        } elseif ($supplyNum > 1) {
            return $error[] = '不要填写多个真实供应商！';
        } else {
            if ($this->input->post('supplierUid') != $providerUser[0]) {
                return $error[] = '该供应商UID（' . $providerUser[0] . '）与商品（' . $this->input->post('supplierUid') . '）不符，请仔细核实。';
            }
        }

        if (!empty($uidArray)) {
            return $error[] = '用户UID为' . implode(array_flip($uidArray), ',') . '的用户不存在';
        }
        return $error;
    }
    
    /**
     * 设置产品上下架
     * @param unknown $goods_id
     * @param unknown $status
     * @param unknown $pageNow
     */
    public function setIsOnSaleStatus($goods_id, $status, $pageNow)
    {
        switch ($status) {
            case '1': $isOnSale = '2'; break;
            case '2': $isOnSale = '1'; break;
            default : $isOnSale = '1'; break;
        }
        $this->db->trans_start();
        $result = $this->tourism_goods_base->setIsOnSaleStatus($goods_id, $isOnSale);
        $this->operate_power_log->insertOperatePowerLog(array('uid'=>$this->uid,'name'=>$this->name.'-商品','record'=>'操作类：'.__CLASS__.'-方法：'.__FUNCTION__, 'content'=>json_encode(array('goods_id'=>$goods_id,'is_on_sale'=>$isOnSale))));
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismgoods/grid/'.$pageNow, $this->input->get(), '操作成功！');
        } else {
            $this->error('tourismgoods/grid/'.$pageNow, $this->input->get(), '操作失败！');
        }
    }
    
    /**
     * 设置产品上下架
     * @param unknown $goods_id
     * @param unknown $status
     * @param unknown $pageNow
     */
    public function setIsLogicDelete($goods_attr_id, $status, $pageNow)
    {
        switch ($status) {
        	case '1': $isLogicDelete = '2'; break;
        	case '2': $isLogicDelete = '1'; break;
        	default : $isOnSale = '1'; break;
        }
        $this->db->trans_start();
        $result = $this->tourism_goods_attr->setIsLogicDelete($goods_attr_id, $isLogicDelete);
         $this->operate_power_log->insertOperatePowerLog(array('uid'=>$this->uid,'name'=>$this->name.'-商品','record'=>'操作类：'.__CLASS__.'-方法：'.__FUNCTION__, 'content'=>json_encode(array('goods_attr_id'=>$goods_attr_id,'isLogicDelete'=>$isLogicDelete))));
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismgoods/grid/'.$pageNow, $this->input->get(), '操作成功！');
        } else {
            $this->error('tourismgoods/grid/'.$pageNow, $this->input->get(), '操作失败！');
        }
    }
    
    /**
     * 设置产品审核状态
     * @param unknown $goods_attr_id
     * @param unknown $status
     * @param unknown $pageNow
     */
    public function setIsCheckStatus($goods_attr_id, $status, $pageNow)
    {
        if (!admin_priv($this->actionList, 'tourism_goods_grid_audit')) {
            $this->error('tourismgoods/grid', $this->input->get(), '产品部才能审核');
        }
        switch ($status) {
            case '1': $isCheck = '2'; break;
            case '2': $isCheck = '3'; break;
            default : $isCheck = '1'; break;
        }
        $this->db->trans_start();
        $result = $this->tourism_goods_attr->setIsCheckStatus($goods_attr_id, $isCheck);
        $this->operate_power_log->insertOperatePowerLog(array('uid'=>$this->uid,'name'=>$this->name.'-商品','record'=>'操作类：'.__CLASS__.'-方法：'.__FUNCTION__, 'content'=>json_encode(array('goods_attr_id'=>$goods_attr_id,'isCheck'=>$isCheck))));
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismgoods/grid/'.$pageNow, $this->input->get(), '操作成功！');
        } else {
            $this->error('tourismgoods/grid/'.$pageNow, $this->input->get(), '操作失败！');
        }
    }
    
     /**
     *批量上下架  审核通过
     */
    public function batchSetStatus()
    {
        if (!admin_priv($this->actionList, 'tourism_goods_grid_audit')) {
            $this->jsen('产品部才能审核');
        }
        $type = (int)$this->input->post('type');
        $pg = (int)$this->input->post('pg');
        $goodsAttrIdArray = $this->input->post('id');
        $goodsIdArray = $this->input->post('goodsId');
        if ( count($goodsIdArray)==0 || count($goodsIdArray)==0 ) {
            $this->jsen('请选择商品');
        }
        $this->db->trans_start();
        if ( in_array($type,array(1,2)) ) {  // 上下架
            $result = $this->tourism_goods_base->setIsOnSaleStatus($goodsIdArray, $type);
        }
        if ( in_array($type,array(3,4))) {  // 审核通过 与 拒绝
            $isCheck = ($type==3) ? 2 : 3;
            $result = $this->tourism_goods_attr->setIsCheckStatus($goodsAttrIdArray, $isCheck);
        }
        if ( in_array($type,array(5))) {  // 逻辑删除
            $result = $this->tourism_goods_attr->setIsLogicDelete($goodsAttrIdArray, $isLogicDelete=2);
        }
        $this->operate_power_log->insertOperatePowerLog(array('uid'=>$this->uid,'name'=>$this->name.'-商品','record'=>'操作类：'.__CLASS__.'-方法：'.__FUNCTION__, 'content'=>json_encode(array('goods_id'=>$goodsIdArray,'goods_attr_id'=>$goodsAttrIdArray,'isCheck'=>$type))));
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->session->set_flashdata('success', '操作成功!');
            $this->jsen(site_url('tourismgoods/grid/'.$pg),true);
        }
        $this->session->set_flashdata('error', '操作失败!');
        $this->jsen('操作失败');
    }
    
    /**
     *批量恢复删除产品
     */
    public function batchBack()
    {
        if (!admin_priv($this->actionList, 'tourism_goods_grid_audit')) {
            $this->jsen('产品部才能审核');
        }
        $type = (int)$this->input->post('type');
        $pg = (int)$this->input->post('pg');
        $goodsAttrIdArray = $this->input->post('id');
        $goodsAttrIdString = implode(',',$goodsAttrIdArray);
        $goodsIdArray = $this->input->post('goodsId');
        if ( count($goodsIdArray)==0 || count($goodsAttrIdArray)==0 ) {
            $this->jsen('请选择商品');
        }
        $this->db->trans_start();
        if ( in_array($type,array(1)) ) {  // 下架
            $result = $this->tourism_goods_base->setIsOnSaleStatus($goodsIdArray, $is_on_sale=2);
        }
        if ( in_array($type,array(2)) ) {  // 逻辑删除
            $result = $this->tourism_goods_attr->setIsLogicDelete($goodsAttrIdArray, $isLogicDelete=1);
        }
        $this->operate_power_log->insertOperatePowerLog(array('uid'=>$this->uid,'name'=>$this->name.'-商品','record'=>'操作类：'.__CLASS__.'-方法：'.__FUNCTION__, 'content'=>$this->name.'恢复了，goods_attr_id 为'.$goodsAttrIdString));
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->session->set_flashdata('success', '操作成功!');
            $this->jsen(site_url('tourismgoods/delete/'.$pg),true);
        }
        $this->session->set_flashdata('error', '操作失败!');
        $this->jsen('操作失败');
    }

    /**
     * ajax 翻页函数部分。
     * @param number $pg
     */
    public function ajaxGetGoodsData($pg = 1)
    {
        $page_num = 20;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('tourismgoods/ajaxGetGoodsData') . $this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismgoods/ajaxGetGoodsData');
        $config['total_rows'] = $this->tourism_goods_base->total($this->input->get());
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_list'] = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_goods_base->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['show_flag'] = array('1' => '大首页', '2' => '商品首页','3' => '热门推荐');
        $data['is_check'] = array('1' => '待审核', '2' => '审核通过', '3' => '审核拒绝');
        $data['is_on_sale'] = array('1' => '上架', '2' => '下架');
        $data['all_region'] = $this->region->getNewRegion();
        $data['province_id'] = $this->input->get('province_id');
        $data['city_id'] = $this->input->get('city_id');
        $data['district_id'] = $this->input->get('district_id');

        echo json_encode(array(
            'status'=>true,
            'html'  =>$this->load->view('tourismgoods/addTourismGoodsId/ajaxTourismGoodsData.php', $data, true)
        ));exit;
    }
    
     /**
     * 根据供应商uid 获取分类
     */
   public function  tourismProviderCategory()
   {
       $provider_uid = trim( $this->input->post('provider_uid',true) );
       $result = $this->tourism_provider_category->providerCategoryByUid($provider_uid);
       $data['providerCategory'] = $result;
       echo $result->num_rows() > 0 ? json_encode(array(
       	   'status'  => true,
           'content' => $this->load->view('tourismgoods/providercategory',$data,true)
       )) : json_encode(array('status'=>false));exit;
   }

    /*------------------------*/

    /**
     * @descripte   获取分类id和名称
     * @Author xiumao
     * @date 2016/5/30 0030 下午 6:48
     */
    public function select_category_name($id)
    {
        $categoryIds = $this->tourism_goods_category->getCategoryById($id);
        echo $categoryIds->num_rows() > 0 ? json_encode(array(
            'status'  => true,
            'content' => $data = array('category_id' => $categoryIds->row()->id, 'category_name' => $categoryIds->row()->full_name),
        )) : json_encode(array('status' => false));exit;
    }

    /**
     * @descripte   获取附加属性
     * @Author xiumao
     * @date 2016/5/30 0030 下午 7:31
     */
    public function ajax_additive()
    {
        $params = $this->input->get();
        $data['additive'] = $this->tourism_goods_category_additive->getAdditive($params['category_id']);
        echo $data['additive']->num_rows() > 0 ? json_encode(array(
            'status'  => true,
            'content' => $this->load->view('tourismgoods/ajaxadditive', $data, true),
        )) : json_encode(array('status' => false, 'messages' => '无附加属性'));exit;
    }

    /**
     * @descripte   商品属性
     * @Author xiumao
     * @date 2016/5/30 0030 下午 7:36
     */
    public function ajax_attribute()
    {
        $params = $this->input->get();
        $data['attribute'] = $this->tourism_goods_category_attribute->getAttributeByCategoryId($params['category_id']);
        echo $data['attribute']->num_rows() > 0 ? json_encode(array(
            'status'  => true,
            'content' => $this->load->view('tourismgoods/ajaxattribute', $data, true),
        )) : json_encode(array('status' => false, 'messages' => '无商品属性'));exit;
    }

    /**
     * @descripte   商品品牌
     * @Author xiumao
     * @date 2016/5/30 0030 下午 7:45
     */
    public function ajax_brand()
    {
        $params = $this->input->get();
        $data['brand'] = $this->tourism_goods_brand->getBrand($params['category_id']);
        echo $data['brand']->num_rows() > 0 ? json_encode(array(
            'status'  => true,
            'content' => $this->load->view('tourismgoods/ajaxbrand', $data, true),
        )) : json_encode(array('status' => false, 'messages' => '无商品品牌'));exit;
    }
}